﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities.Enums;

namespace Interview_Prov.Private.Moder
{
    public partial class ChgStateInterview : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetInterviewID() == -1)
            {
                Response.Redirect("~\\Private\\User\\SelectInterview.aspx?ReturnUrl=\\Private\\Moder\\ChgStateInterview.aspx");
            }
            else
            {
                int id = GetInterviewID();
                Business.BusinessLogic bll = new Business.BusinessLogic();
                var interv=bll.LoadInterview(id);
                if (interv.ID==-1)
                {
                    Response.Redirect("~/Errors/InterviewLoadError.aspx");
                    return;
                }

                if (!IsPostBack)
                {
                    RadioButtonList1.SelectedIndex = (int)bll.GetInterviewState(id);
                }
                else
                {
                    foreach (var quest in interv.Questions)
                    {
                        if (quest.Answers.Count() < 2 && quest.Type != QuestionType.TextInput)
                        {
                            Response.Redirect("~/Errors/AnswerCountError.aspx");
                            return;
                        }
                    }
                    bll.SetInterviewState(id,(InterviewState)RadioButtonList1.SelectedIndex);
                }
            }
        }

        protected int GetInterviewID()
        {
            int iid = -1;
            if (Request.Params["InterviewID"] != null)
            {
                int.TryParse(Request.Params["InterviewID"], out iid);
            }
            return iid;
        }
    }
}